/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.rum.gui.component;

import cz.muni.fi.rum.domainmodel.Reflector;
import cz.muni.fi.rum.domainmodel.ReflectorListener;
import cz.muni.fi.rum.domainmodel.ReflectorSession;
import cz.muni.fi.rum.sender.Factory;
import cz.muni.fi.rum.sender.SenderException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author pmikulasek
 */
public class TreeModel extends DefaultTreeModel {

    private Reflector reflector;
    private final FolderNode rootNode;
    private final FolderNode sessions = new FolderNode("Sessions & Listeners");
    private final FolderNode processors = new FolderNode("Processors");

    public static enum ReflectorNodeType {

        folder, listener, session
    }

    public TreeModel() {
        super(new FolderNode("Reflector"));
        rootNode = (FolderNode) super.getRoot();
        rootNode.add(sessions);
        rootNode.add(processors);
        //TODO === TREE MODEL === dodelat aktualizaci podle reflectoru.
//        reflector = ReflectorFactoryImpl.getInstance().getOrCreateReflector();
    }

    public void setReflector(Reflector reflector) {
        this.reflector = reflector;
    }

    /**
     * Zde se vytvori novy Listener
     * @return
     */
    public ListenerNode createNewReflectorListener() {
        ReflectorSession reflectorSession = new ReflectorSession();
        ReflectorListener reflectorListener = new ReflectorListener();
        reflectorSession.addReflectorListener(reflectorListener);
        SessionNode sessNode = new SessionNode();
        ListenerNode listNode = new ListenerNode();
        sessNode.setContent(reflectorSession);
        listNode.setContent(reflectorListener);
        reflector.addSession(reflectorSession);
        sessNode.add(listNode);
        sessions.add(sessNode);
        super.insertNodeInto(sessNode, sessions, 0);
        return listNode;
    }

    /**
     * Metoda odebere vsechny session Node.
     * @param nodes
     */
    public void removeSessionNodes(SessionNode[] nodes) {
        for (int i = 0; i < nodes.length; i++) {
            reflector.removeSession((ReflectorSession) nodes[i].getContent());
            this.removeNodeFromParent(nodes[i]);
        }
    }

    /**
     * Metoda odebere listenery z jejich session a vytvori RapRequest pro zastaveni techto listeneru.
     * @param nodes
     */
    public void removeListenerNodes(ListenerNode[] nodes) {
        for (int i = 0; i < nodes.length; i++) {
            SessionNode parent = (SessionNode) nodes[i].getParent();
            ReflectorSession session = (ReflectorSession) parent.getContent();
            ReflectorListener listener = (ReflectorListener) nodes[i].getContent();
            session.removeReflectorListener(listener);
            this.removeNodeFromParent(nodes[i]);
        }
    }

    /**
     * Metoda vlozi vsechny listenery do jedne session na dany index. Dale vytvori RapRequest pro spojeni do jedne session.
     * @param listeners
     * @param session
     * @param index
     */
    public void addListenerNodes(ListenerNode[] nodes, SessionNode parent, int index) {
        ReflectorSession session = (ReflectorSession) parent.getContent();
        for (int i = 0; i < nodes.length; i++) {
            session.addReflectorListener((ReflectorListener) nodes[i].getContent());
            this.insertNodeInto(nodes[i], parent, index++);
        }
    }

    public void actualize() {
        if (sessions.getChildCount() == reflector.size()) {
            System.out.println("Stejny pocet session");
        }
        Collection<ReflectorSession> sessionsList = reflector.getAllReflectorSessions();
        for (int i = 0; i < sessions.getChildCount(); i++) {
            SessionNode child = (SessionNode) sessions.getChildAt(i);
            ReflectorSession content = (ReflectorSession) child.getContent();
            if (sessionsList.contains(content)) {
                if (child.getChildCount() == content.getAllListeners().size()) {
                    System.out.println("Stejny pocet listeneru v session");
                }
                Collection<ReflectorListener> listeners = content.getAllListeners();
                for (int j = 0; j < child.getChildCount(); j++) {
                    ListenerNode listNode = (ListenerNode) child.getChildAt(j);
                    if (listeners.contains(listNode.getContent())) {
                    } else {
                        System.err.println("Chybi listener");
                    }
                }
            } else {
                System.err.println("Chybi session");
            }
        }

//        if (!sessionsList.isEmpty()) {
//            for (ReflectorSession session : sessionsList) {
//                Set<ReflectorListener> listeners = session.getListeners();
//                if (!listeners.isEmpty()) {
//                    SessionNode sessionNode = new SessionNode("Session");
//                    sessionNode.setContent(session);
//                    for (ReflectorListener listener : listeners) {
//                        StringBuffer stringBuffer = new StringBuffer();
//                        stringBuffer.append("Listener - ");
//                        stringBuffer.append(listener.getType());
//                        stringBuffer.append("/");
//                        stringBuffer.append(listener.getPortNumber());
//                        ListenerNode listenerNode = new ListenerNode(stringBuffer.toString());
//                        listenerNode.setContent(listener);
//                        sessionNode.add(listenerNode);
//                    }
//                    super.insertNodeInto(sessionNode, sessions, 0);
//                }
//            }
//        }
    }

    public void removeNode() {
    }
//    public void addReflectorSession(ReflectorSession sess) {
////        reflector.add(listener);
//    }
}
